package xps.and.uudaijia.userclient.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.data.baen.CodeBean;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.util.AppCommond;
import xps.and.uudaijia.userclient.view.base.BaseActivity;

/**
 * Created by XPS on 2017/9/2.
 */

public class CancelActivity_v2 extends BaseActivity {
        @BindView(R.id.actionbar_iv_back)
        ImageView actionbarIvBack;
        @BindView(R.id.actionbar_tv_title)
        TextView actionbarTvTitle;
        @BindView(R.id.actionbar_tv_right)
        TextView actionbarTvRight;
        @BindView(R.id.v_gap_line)
        View vGapLine;
        @BindView(R.id.actionbar_al)
        AutoRelativeLayout actionbarAl;
        @BindView(R.id.cb_1)
        CheckBox cb1;
        @BindView(R.id.al_1)
        AutoLinearLayout al1;
        @BindView(R.id.cb_2)
        CheckBox cb2;
        @BindView(R.id.al_2)
        AutoLinearLayout al2;
        @BindView(R.id.cb_3)
        CheckBox cb3;
        @BindView(R.id.al_3)
        AutoLinearLayout al3;
        @BindView(R.id.cb_4)
        CheckBox cb4;
        @BindView(R.id.al_4)
        AutoLinearLayout al4;
        @BindView(R.id.cb_5)
        CheckBox cb5;
        @BindView(R.id.al_5)
        AutoLinearLayout al5;
        @BindView(R.id.et_reason)
        EditText etReason;
        @BindView(R.id.bt_cancel_order)
        Button btCancelOrder;

        String orderID = "";
        String textReason = "没有司机接单";
    String panduan="";
    @Override
    protected int getLayout() {
        return R.layout.activity_cancel_v2;
    }

    @Override
    protected void setData() {

    }

    void unCheck() {
        cb1.setChecked(false);
        cb2.setChecked(false);
        cb3.setChecked(false);
        cb4.setChecked(false);
        cb5.setChecked(false);
        etReason.setVisibility(View.GONE);
    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
        Intent recIntent = getIntent();
        orderID = recIntent.getStringExtra("orderID");
        actionbarTvTitle.setText("销单");
        panduan = recIntent.getStringExtra("panduan");
    }

    @OnClick({R.id.al_1, R.id.al_2, R.id.al_3, R.id.al_4, R.id.al_5,R.id.bt_cancel_order,R.id.actionbar_iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.al_1:
                unCheck();
                cb1.setChecked(true);
                etReason.setVisibility(View.GONE);
                textReason = "没有司机接单";
                break;
            case R.id.al_2:
                unCheck();
                cb2.setChecked(true);
                etReason.setVisibility(View.GONE);
                textReason = "等司机太久";
                break;
            case R.id.al_3:
                unCheck();
                cb3.setChecked(true);
                etReason.setVisibility(View.GONE);
                textReason = "司机不接电话";
                break;
            case R.id.al_4:
                unCheck();
                cb4.setChecked(true);
                etReason.setVisibility(View.GONE);
                textReason = "暂时不想要司机";
                break;
            case R.id.al_5:
                unCheck();
                cb5.setChecked(true);
                etReason.setVisibility(View.VISIBLE);
                textReason = "其他原因";
                break;
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.bt_cancel_order:
                cancelOrder();
                break;
        }
    }

    void cancelOrder() {
        if(etReason.getVisibility() == View.VISIBLE)
            textReason = etReason.getText().toString();
        UserNetWorks.getMapQuXiao(orderID, textReason, "1", new Subscriber<CodeBean>() {
                    @Override
                    public void onCompleted() {
                        dissmisDialog();
                    }
                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        dissmisDialog();
                    }

                    @Override
                    public void onNext(CodeBean codeBean) {
                        if (codeBean.getReturn_code().equals("SUCCESS")) {
                            processPrivousPage();
                            JUtils.Toast(codeBean.getReturn_msg());
                            dissmisDialog();
                            finish();
                        } else {
                            JUtils.Toast(codeBean.getReturn_msg());
                            dissmisDialog();
                        }
                    }
                }
        );
    }

    void processPrivousPage() {
        if (panduan.equals("1")) {
            AppCommond appCommond = new AppCommond.Builder()
                    .setOpration(AppCommond.CLOSE_ACTIVITY)
                    .setRequestcloseAcname(MapActivity.class.getSimpleName())
                    .build();
            EventBus.getDefault().post(appCommond);
        } else if (panduan.equals("2")) {
            AppCommond appCommond = new AppCommond.Builder()
                    .setOpration(AppCommond.CLOSE_ACTIVITY)
                    .setRequestcloseAcname(MapViewActivity.class.getSimpleName())
                    .build();
            EventBus.getDefault().post(appCommond);
        }
    }

}
